package com.wuliu.system.service.rabbitmq;

import com.wuliu.common.constant.RabbitMQConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * 消息生产者服务
 *
 * @author realue
 * @version 1.0
 * @date 2024-04-24 11:33
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RabbitMQService {

    private final RabbitTemplate rabbitmqTemplate;

    /**
     * @param smsId 短信记录id
     */
    public void send(String smsId) {
        rabbitmqTemplate.convertAndSend(RabbitMQConstants.SMS_EXCHANGE_NAME, "", smsId);
    }

    /**
     * 发送订单延迟消息
     *
     * @param orderId 订单id
     * @param delay 延迟时间，单位：秒
     */
    public void sendOrderDelay(String orderId,Integer delay) {
        rabbitmqTemplate.convertAndSend(RabbitMQConstants.ORDER_DELAY_EXCHANGE_NAME, "", orderId, message -> {
            message.getMessageProperties().setDelay(1000 * delay);
            return message;
        });
        log.info("订单延迟消息发送成功，订单id：{}", orderId);
    }

}
